package event.handlers;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import serializare.SerializationToXml;
import bean.Person;
import core.controller.EventHandler;
import event.SessionKeys;

public class DisplaySearchEventHandler extends EventHandler {
    private List<Person> persons = SerializationToXml.deserializeXMLToObject();
    private Set<Person> search = new LinkedHashSet<Person>();

    @Override
    public String perform() {
        String nextUrl = getNextURL("OK");
        int id = event.getInt("id");
        String lastName = event.getString("lastName");
        String firstName = event.getString("firstName");
        String ageSearch = event.getString("age");
        try {
            for (ListIterator<Person> iter = persons.listIterator(); iter.hasNext();) {
                Person pers = iter.next();
                if (isPersonPassingFilters(pers, id, firstName, lastName, ageSearch)) {
                    search.add(pers);
                }
                session.setAttribute(SessionKeys.SEARCH_PERSONS, search);
            }
        } catch (NullPointerException e) {
            nextUrl = getNextURL("ERROR");
        } catch (IllegalStateException e) {
            nextUrl = getNextURL("ERROR");
        } catch (NumberFormatException e) {
            nextUrl = getNextURL("ERROR");
        }
        return nextUrl;
    }

    private boolean isPersonPassingFilters(Person person, int id, String firstName, String lastName, String ageSearch) {
        if (person == null) {
            return false;
        } else if (id != 0 && person.getId() != id) {
            return false;
        } else if (person.getLastName().indexOf(lastName) == -1) {
            return false;
        } else if (person.getFirstName().indexOf(firstName) == -1) {
            return false;
        } else if (isValidAge(ageSearch)) {
            String operator = getAgeOperator(ageSearch);
            int age = getAge(ageSearch);
            if ("<".equals(operator)) {
                if (person.getAge() >= age) {
                    return false;
                }
            } else if (">".equals(operator)) {
                if (person.getAge() <= age) {
                    return false;
                }
            }
        }
        return true;
    }
    

    private boolean isValidAge(String age) {
        if (StringUtils.isEmpty(age)) {
            return false;
        }
        if (age.startsWith(">") || age.startsWith("<")) {
            if (age.length() == 1) {
                return false;
            }
            try {
                Integer.valueOf(age.substring(1, age.length()));
            } catch (NumberFormatException nfe) {
                return false;
            }
        }
        return true;
    }

    private String getAgeOperator(String age) {
        if (StringUtils.isEmpty(age)) {
            return null;
        }
        if (age.charAt(0) == '<' || age.charAt(0) == '>') {
            return age.substring(0, 1);
        }
        return null;
    }

    private int getAge(String age) {
        return Integer.valueOf(selectAge(age));
    }

    private String selectAge(String ageSearch) {
        if (ageSearch.startsWith(">")) {
            ageSearch = ageSearch.replace(">", "");
        } else if (ageSearch.startsWith("<")) {
            ageSearch = ageSearch.replace("<", "");
        }
        return ageSearch;
    }
}
